import {
  resetSearchResultList,
  refreshSearchResultList,
  appendSearchResultList,
} from '../reducers/search-result-list';
import HttpClient from '../commons/http-client';
import {FixedParams, ProductType, RemvArchParams} from '../reducers/constant';
import {updateSearchResultMap} from '../reducers/search-result-map';
import {LoadingStatus, updateAppState} from '../reducers/app-state';
import Toast from '../components/toast';
import {I18n} from '../reducers/intl';
import {signOut} from './auth-actions';

export function search(filterParams, page = 1, isMap) {
  return async dispatch => {
    const params = {
      avail_weeks: filterParams.avail,
      ordering: filterParams.ordering ? `-${filterParams.ordering}` : '',
      search: filterParams.keywords,
      expand: 'busy_dates,avatar',
      product_name: filterParams.product,
    };
    if (isMap) {
      params.no_page = 'no_page';
    } else {
      params.page = page;
      params.page_size = 10;
    }
    if (filterParams.type === ProductType.fixed) {
      if (filterParams[FixedParams.materials]) {
        params.fixed_products__materials__in = filterParams[
          FixedParams.materials
        ].join(',');
      }
      if (filterParams[FixedParams.metal_types]) {
        params.fixed_products__metal_types__in = filterParams[
          FixedParams.metal_types
        ].join(',');
      }
      if (filterParams[FixedParams.margins]) {
        params.fixed_products__margins__in = filterParams[
          FixedParams.margins
        ].join(',');
      }
      if (filterParams[FixedParams.pontics]) {
        params.fixed_products__pontics__in = filterParams[
          FixedParams.pontics
        ].join(',');
      }
      if (filterParams[FixedParams.implants]) {
        params.fixed_products__implants__in = filterParams[
          FixedParams.implants
        ].join(',');
      }
      if (filterParams[FixedParams.abutments]) {
        params.fixed_products__abutments__in = filterParams[
          FixedParams.abutments
        ].join(',');
      }
      if (filterParams[FixedParams.colors]) {
        params.fixed_products__colors__in = filterParams[
          FixedParams.colors
        ].join(',');
      }
      if (filterParams[FixedParams.others]) {
        params.fixed_products__others__in = filterParams[
          FixedParams.others
        ].join(',');
      }
    } else {
      if (filterParams[RemvArchParams.metals]) {
        params.remv_arch_products__metals__in = filterParams[
          RemvArchParams.metals
        ].join(',');
      }
      if (filterParams[RemvArchParams.waxes]) {
        params.remv_arch_products__waxes__in = filterParams[
          RemvArchParams.waxes
        ].join(',');
      }
      if (filterParams[RemvArchParams.colors]) {
        params.remv_arch_products__colors__in = filterParams[
          RemvArchParams.colors
        ].join(',');
      }
      if (filterParams[RemvArchParams.others]) {
        params.remv_arch_products__others__in = filterParams[
          RemvArchParams.others
        ].join(',');
      }
    }

    const url = `/web/search-vendor?${HttpClient.encodeParams(params)}`;
    // console.log('filterParams', params);
    try {
      dispatch(updateAppState({loading: LoadingStatus.PENDING}));
      const listRep = await HttpClient.get(url);
      dispatch(updateAppState({loading: LoadingStatus.DONE}));
      const payload = listRep.data.data;
      // const names = payload.results.map(r => r.vendor_name);
      // console.log(names);
      // console.log('payload', JSON.stringify(payload));
      if (isMap) {
        dispatch(updateSearchResultMap(payload));
      } else {
        if (page === 1) {
          dispatch(
            refreshSearchResultList({
              results: payload.results,
              current: payload.current,
              total: payload.count,
            }),
          );
        } else {
          dispatch(
            appendSearchResultList({
              results: payload.results,
              current: payload.current,
              total: payload.count,
            }),
          );
        }
      }
    } catch (e) {
      dispatch(updateAppState({loading: LoadingStatus.ERROR}));
      if (e.message === 'Request failed with status code 404') {
        Toast.show(I18n.t('toast.noMore'));
      } else if (e.message === 'Request failed with status code 401') {
        dispatch(signOut());
      }
    }
  };
}

export function reset(payload) {
  return dispatch => {
    dispatch(resetSearchResultList(payload));
  };
}
